﻿namespace NuGetCop.MSBuild.Tasks.Configuration.Readers
{
    using System.Diagnostics.Contracts;
    using System.IO;
    using System.Xml;

    internal sealed class NuGetCopSettingsXmlStreamReader : INuGetCopSettingsReader<Stream>
    {
        private readonly XmlReaderSettings readerSettings;
        private readonly INuGetCopSettingsReader<XmlReader> xmlSettingsReader;

        public NuGetCopSettingsXmlStreamReader(
            XmlReaderSettings readerSettings,
            INuGetCopSettingsReader<XmlReader> xmlSettingsReader)
        {
            Contract.Requires(readerSettings != null);
            Contract.Requires(xmlSettingsReader != null);

            this.readerSettings = readerSettings;
            this.xmlSettingsReader = xmlSettingsReader;
        }

        public NuGetCopSettings ReadSettings(Stream source)
        {
            NuGetCopSettings resultSettings;

            using (var xmlReader = XmlReader.Create(source, readerSettings))
            {
                resultSettings = xmlSettingsReader.ReadSettings(xmlReader);
            }
 
            return resultSettings;
        }
    }
}
